﻿using System;
using System.Collections.Generic;
using NUnit.Framework;
using Xenta.Enums;
using Xenta.Operations;

namespace Xenta.Tests.Operations
{
    [TestFixture]
    public class FileOwnerTests : OperationTest
    {
        protected override IEnumerable<Type> ModelMappings
        {
            get
            {
                return NHMappings.ALL;
            }
        }

        [TestCase(1)]
        public void Can_CRUD(long quota)
        {
            #region Prepare

            var country = Processor.Execute(new CreateCountry
            {
                Code = "xy"
            }).Result;
            var localizationProfile = Processor.Execute(new CreateLocalizationProfile
            {
                Code = "xy",
                LanguageID = Processor.Execute(new CreateLanguage
                {
                    Code = "xy"
                }).Result.EntityID,
                TimeZoneID = Processor.Execute(new CreateTimeZone
                {
                    Code = "xy"
                }).Result.EntityID,
                CurrencyID = Processor.Execute(new CreateCurrency
                {
                    Code = "xy",
                    Rate = 1
                }).Result.EntityID,
                CountryID = country.EntityID,
                RegionID = Processor.Execute(new CreateRegion
                {
                    Code = "xy",
                    CountryID = country.EntityID
                }).Result.EntityID,
                DimensionUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-dim",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Dimension
                }).Result.EntityID,
                WeightUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-weight",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Weight
                }).Result.EntityID,
                InformationUnitID = Processor.Execute(new CreateMeasureUnit
                {
                    Code = "xy-info",
                    Ratio = 1,
                    Flags = MeasureUnitFlags.Information
                }).Result.EntityID
            }).Result;
            var account = Processor.Execute(new CreateAccount
            {
                Email = "email@example.com",
                Username = "username",
                Password = "password",
                LocalizationProfileID = localizationProfile.EntityID
            }).Result;

            #endregion

            var owner = Processor.Execute(new CreateFileOwner
            {
                AccountID = account.EntityID,
                Quota = quota
            }).Result;

            Assert.NotNull(owner);
            owner.Quota.AreEqual(quota);
            owner.AreEqual(Processor.Execute(new GetFileOwner
            {
                EntityID = owner.EntityID
            }).Result);
            owner.AreEqual(Processor.Execute(new GetFileOwnerByAccount
            {
                AccountID = account.EntityID
            }).Result);

            quota = quota * 2;
            var upd_owner = Processor.Execute(new UpdateFileOwner
            {
                EntityID = owner.EntityID,
                Quota = quota
            }).Result;

            Assert.NotNull(upd_owner);
            upd_owner.AreEqual(owner);
            upd_owner.Quota.AreEqual(quota);

            Processor.Execute(new DeleteFileOwner
            {
                EntityID = owner.EntityID
            });
        }

        [TestCase(-1)]
        public void Cannot_Create_If_Quota_Lt_Zero(long quota)
        {
            Assert.Throws<ApplicationException>(() => Processor.Execute(new CreateFileOwner
            {
                Quota = quota
            }));
        }
    }
}
